{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube('cORcGGH03gg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analyzing National Land Cover Database (NLCD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add NLCD data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = ee.Image('USGS/NLCD/NLCD2016')\n",
    "landcover = ee.Image(dataset.select('landcover'))\n",
    "Map.addLayer(landcover, {}, 'NLCD 2016')\n",
    "\n",
    "states = ee.FeatureCollection(\"TIGER/2018/States\")\n",
    "Map.addLayer(states, {}, 'US States')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_legend(builtin_legend='NLCD')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculate land cover compostion of each US state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n",
    "nlcd_stats = os.path.join(out_dir, 'nlcd_stats.csv')  \n",
    "\n",
    "# statistics_type can be either 'SUM' or 'PERCENTAGE'\n",
    "# denominator can be used to convert square meters to other areal units, such as square kilimeters\n",
    "geemap.zonal_statistics_by_group(landcover, states, nlcd_stats, statistics_type='SUM', denominator=1000000, decimal_places=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Analyzing Global Land Cover"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Add MODIS global land cover data\n",
    "\n",
    "MODIS MCD12Q1.051 Land Cover Type Yearly Global 500m \n",
    "\n",
    "https://developers.google.com/earth-engine/datasets/catalog/MODIS_051_MCD12Q1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "landcover = ee.Image('MODIS/051/MCD12Q1/2013_01_01') \\\n",
    "    .select('Land_Cover_Type_1')\n",
    "\n",
    "Map.setCenter(6.746, 46.529, 2)\n",
    "Map.addLayer(landcover, {}, 'MODIS Land Cover')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.add_legend(builtin_legend='MODIS/051/MCD12Q1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "countries_shp = '../data/countries.shp'\n",
    "countries = geemap.shp_to_ee(countries_shp)\n",
    "Map.addLayer(countries, {}, 'Countries')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n",
    "global_stats = os.path.join(out_dir, 'global_stats.csv')  \n",
    "\n",
    "# statistics_type can be either 'SUM' or 'PERCENTAGE'\n",
    "# denominator can be used to convert square meters to other areal units, such as square kilimeters\n",
    "geemap.zonal_statistics_by_group(landcover, countries, global_stats, statistics_type='PERCENTAGE', denominator=1000000, decimal_places=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
